package com.ruoyi.project.cms.type.contorller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.exception.cms.CmsNotDeleteException;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.cms.type.domain.CmsType;
import com.ruoyi.project.cms.type.service.ICmsTypeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @program: zhizun
 * @author: Mrs.ye
 * @create: 2020-09-15 15:05
 * @description: 系统公告
 **/
@Controller
@RequestMapping("/cms/type")
public class CMSTypeController extends BaseController {
    //声明资源地址的公共前缀
    private String prefix = "cms/type";

    @Autowired
    private ICmsTypeService cmsTypeService;

    /**
     * 跳转公告类型管理页面
     *
     * @return 跳转到类型的主页
     */
    @RequiresPermissions("cms:type:view")
    @GetMapping()
    public String cmsType() {
        return prefix + "/type";
    }


    /**
     * 跳转公告类型添加页面
     *
     * @return 跳转到类型的添加页的地址
     */
    @RequiresPermissions("cms:type:add")
    @GetMapping("/add")
    public String cmsTypeadd() {
        return prefix + "/add";
    }

    /**
     * 分页查询公告和条件查询公告
     *
     * @param cmsType
     * @return
     */

    @RequiresPermissions("cms:type:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CmsType cmsType) {
        startPage();
        List<CmsType> list = cmsTypeService.selectCmsTypeList(cmsType);
        return getDataTable(list);
    }

    /**
     * 校验公告名称
     */
    @PostMapping("/checkTypeNameUnique")
    @ResponseBody
    public String checkTypeNameUnique(CmsType cmsType) {
        return cmsTypeService.checkTypeNameUnique(cmsType);
    }

    /**
     * 添加公告的操作
     *
     * @param cmsType
     * @return
     */

    @RequiresPermissions("cms:type:add")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add(CmsType cmsType) {

        if (UserConstants.TYPE_CODE_NOT_UNIQUE.equals(cmsTypeService.checkTypeNameUnique(cmsType))) {
            return error("新增公告'" + cmsType.getTypeName() + "'失败，公告名称已存在");
        }

        return toAjax(cmsTypeService.insertCmsType(cmsType));
    }

    /**
     * 修改公告时的数据回显操作
     *
     * @param typeId
     * @param mmap
     * @return 1)根据id到数据库中查询对应的id,并保存在modelMap中
     * 2)跳转到修改的页面,进行数据回显
     */
    @GetMapping("/edit/{typeId}")
    public String edit(@PathVariable("typeId") Long typeId, ModelMap mmap) {


        mmap.put("cmsType", cmsTypeService.selectTypeById(typeId));
        return prefix + "/edit";
    }

    /**
     * @param cmsType
     * @return
     */
    @RequiresPermissions("cms:view:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated CmsType cmsType) {
        if (UserConstants.TYPE_CODE_NOT_UNIQUE.equals(cmsTypeService.checkTypeNameUnique(cmsType))) {
            return error("修改公告'" + cmsType.getTypeName() + "'失败，公告名称已存在");
        }

        return toAjax(cmsTypeService.updateType(cmsType));
    }

    /**
     * 删除公告的操作
     *
     * @param ids
     * @return
     */
    @RequiresPermissions("cms:view:remove")
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(Long[] ids) {

        try {
            int result = cmsTypeService.removeCmsTypeById(ids);
            return toAjax(result);
        } catch (CmsNotDeleteException e) {
            return AjaxResult.error(e.getMessage());
        }
    }


}
